package com.hpin.mlxteacher.child;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.hpin.mlxteacher.R;
import com.hpin.mlxteacher.adapter.ChildAdapter;
import com.hpin.mlxteacher.base.BaseActivity;
import com.hpin.mlxteacher.bean.ChildResult;
import com.hpin.mlxteacher.utils.Constant;
import com.hpin.mlxteacher.utils.HttpUtil;

//import com.umeng.analytics.MobclickAgent;


/**
 * 孩子列表页面
 * Created by Aaron on 15/7/29.
 */
public class ChildListActivity extends BaseActivity {
    //    private List<ChildResult.childlist> tempList = new ArrayList<ChildResult.childlist>();
    private ChildAdapter adapter;
    private ListView lv_child;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_child_list);

//        tempList = (List<ChildResult.childlist>) getIntent().getSerializableExtra("childDatas");

        initWidget();

        getChildData();
//        //注册EventBus
//        EventBus.getDefault().register(this);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//
        mApplication.cancelPendingRequests(getClass().getName());

//        //反注册EventBus
//        EventBus.getDefault().unregister(this);
    }

//    public void onEventMainThread(ChildResult childResult) {
//
//
//        LogUtil.e("tag", "===onEventMainThread===");
//
//        if (childResult!=null) {
//
//            if (childResult.data!=null) {
//
//                adapter = new ChildAdapter(this, childResult.data);
//                lv_child.setAdapter(adapter);
//
//            }else {
//                showToast("没有孩子数据");
//            }
//
//        }
//
//    }


    private void initWidget() {

        TextView tv_left = (TextView) findViewById(R.id.tv_left);
        TextView tv_title = (TextView) findViewById(R.id.tv_title);
        TextView tv_right = (TextView) findViewById(R.id.tv_right);
        tv_left.setText(getResources().getString(R.string.work));
        tv_title.setText(getResources().getString(R.string.rl_child));
        tv_right.setText(getResources().getString(R.string.map));

        lv_child = (ListView) findViewById(R.id.lv_child);

        tv_left.setOnClickListener(this);
        tv_right.setOnClickListener(this);
    }

    @Override
    public void widgetClick(View v) {
        switch (v.getId()) {

            case R.id.tv_left:

                Intent intent = new Intent(this, WorkActivity.class);
                startActivity(intent);
                finish();

                break;
            case R.id.tv_right:
                finish();

                break;

        }
    }

    /**
     * 获取孩子数据
     */
    private void getChildData() {

        JSONObject j = new JSONObject();
        j.put("token", sp.getString(Constant.token, ""));
        j.put("lat", mApplication.myLatitude);
        j.put("lon", mApplication.myLongitude);
        j.put("classId", sp.getString(Constant.classId, ""));

        HttpUtil.setPostRequest("刷新中...", this, Constant.WEB_ROOT + Constant.GETCHILDERNANDWATCH,
                j.toJSONString(), this.getClass().getName(), new HttpUtil.SuccessListener() {
            @Override
            public void setOnSuccessListener(String str) {

                try {
                    ChildResult childResult = JSONObject.parseObject(str, ChildResult.class);
                    if (childResult.code == Constant.SUCCESS) {

                        adapter = new ChildAdapter(ChildListActivity.this, childResult.data);
                        lv_child.setAdapter(adapter);

                    } else {
                        showToast(Constant.showErrorMsg(childResult.code, ChildListActivity.this));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    showToast(Constant.NODATA);
                }
            }
        }, null);
    }

}
